package cn.kgc.javaweb.servlet;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.lang.model.element.VariableElement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/3/22
 */
@WebServlet("/register")
public class RegisterServlet  extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        //  1.判定请求是否是上传请求
        boolean multipartContent = ServletFileUpload.isMultipartContent(req);
        if(multipartContent){  // true 表示文件上传

            //创建FileItemFactory 设置文件上传的基础参数  默认  interface  实现类 DiskFileItemFactory
            DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
            //获取解析文件上传请求的对象
            ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
            try {
                List<FileItem> fileItems = servletFileUpload.parseRequest(req);
                //遍历集合  查看是普通数据 还是文件上传数据
                fileItems.forEach(fileItem -> {
                    //判定当前参数是否是普通表单项
                    boolean formField = fileItem.isFormField();
                    if(formField){ //普通表单项
                        //获取参数的key
                        String fieldName = fileItem.getFieldName();
                        if("username".equals(fieldName)){

                            try {
                                byte[] bytes = fileItem.getString().getBytes("iso8859-1");
                                String s = new String(bytes, "utf-8");
                                System.out.println("username = " + s);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }else if("password".equals(fieldName)){
                            System.out.println("password= " + fileItem.getString());
                        }

                        // TODO  将用户名和密码写入数据库

                    }else{  //文件上传

                        //获取文件名
                        String name = fileItem.getName();
                        // TODO  生成UUID 作为文件的名字  避免文件名重复 造成覆盖  uuid+文件后缀

                        // TODO    将文件的真实名字和对应的uuid名 写入数据库

                        //指定文件上传路径
                        String path = "D:\\upload";
                        //创建file对象
                        File file = new File(path, name);
                        try {
                            fileItem.write(file);
                            System.out.println("文件上传完毕");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

            } catch (FileUploadException e) {
                e.printStackTrace();
            }

        }

    }
}
